yjx.controller('editRelationController',['$rootScope','$scope','$fetch','modalService','$stateParams','$timeout',function ($rootScope,$scope,$fetch,modalService,$stateParams,$timeout) {
    var clientListApi='/customer/relation/selected';
    //$scope.$parent.customerId = $stateParams.customerId;
    //$scope.customerName = $stateParams.customerName;
    $rootScope.tabCtrlName = $rootScope.tabCtrlNameArr[3];
    /*获取客户列表*/
    function getClientList() {
        $fetch.fetch(
            clientListApi,
            {
                params:{
                    userId:$rootScope.userId,
                    customerId:$scope.$parent.customerId
                },
                type:'POST',
                cache:false,
            }
        ).then(function (data) {
                $timeout(function(){
                    if(data && data.selecteds) {
                        $scope.clientList = data.selecteds;
                    }
                })
            }).catch(function (e) {
                modalService.mainMsgError(e.msg);
            })
    }
    if($scope.$parent.customerId){
        getClientList();
    }


    /*删除关系*/
    $scope.deleteRelation = function (id) {
        $scope.deleteId = id;
        modalService.confirm("是否删除该用户关系",confirm)
    }
    function confirm() {
        $fetch.fetch(
            '/customer/relation/remove',
            {
                params:{
                    userId:$rootScope.userId,
                    relationId:$scope.deleteId,
                },
                type:'POST'
            }).then(function(data){
                $timeout(function(){
                    console.log($scope.deleteId)
                    if( $scope.deleteId ) {
                        for(var i in $scope.$parent.relationList ) {
                            if($scope.deleteId  == $scope.$parent.relationList[i].relationId) {
                                console.log($scope.$parent.relationList[i]);
                                if( $scope.clientList &&  $scope.clientList.length>0) {
                                    $scope.clientList.push($scope.$parent.relationList[i]);
                                }else{
                                    $scope.clientList = new Array($scope.$parent.relationList[i]);
                                }
                                $scope.$parent.relationList.splice(i,1);
                                return;
                            }
                        }
                    }
                })
                }).catch(function(e){
                modalService.mainMsgError(e.msg)
            })
    }
    /*新增客户关系*/
    $scope.newRelationAct = function () {
        $timeout(function(){
            if($scope.clientList && $scope.clientList.length>0) {
                $scope.addRelation = true;
                $rootScope.modalTag = 'addRelationModal';
                $rootScope.modalStatus = 'modal-show';
            }else{
                modalService.mainMsgInfo( "当前没有可选客户哦");

            }
        })

    }
    /*请选择客户模态框*/
    $scope.selectedClient = {userId:null,customerName:null,relationName:null};
    $scope.addRelation = false;
    $scope.addRelationAct = function () {
        if($scope.selectedClient && $scope.selectedClient.id > 0 ){
            $scope.addRelation = false;
            $scope.inputRelation = true;
            $rootScope.modalTag = 'inputRelationModal';
            $rootScope.modalStatus = 'modal-show';
        }else{
            $scope.addRelation = false;
            $rootScope.modalStatus = 'modal-hide';
        }


        //console.log($scope.selectedClient);
    }
    $scope.changeClient = function (item) {
        //console.log(item);
        $scope.selectedClient.id = item.id;
        $scope.selectedClient.customerName = item.customerName;
    }
    /*请输入与目标客户的关系*/
    $scope.inputRelation = false;
    $scope.inputRelationAct = function () {
        if(!$scope.relationName){
            /*没填关系名称*/
            $rootScope.modalStatus = 'modal-hide';
            $scope.inputRelation = false;
            return
        }
        $rootScope.modalTag = '';
        $rootScope.modalStatus = 'modal-hide';
        $scope.inputRelation = false;
        $scope.selectedClient.relationName = $scope.relationName;
        $fetch.fetch(
            '/customer/relation/add',
            {
                params:{
                    userId:$rootScope.userId,
                    relationName:$scope.selectedClient.relationName,
                    id:$scope.selectedClient.id,
                    customerParentId:$scope.$parent.customerId
                },
                type:'POST'
            }
        ).then(function(data){
                $timeout(function(){
                    inputSuc(data);
                })
            }).catch(function(e){
                modalService.mainMsgError(e.msg);
            })
    }

    /*添加成功*/
    function inputSuc(data){
        if($scope.clientList){
            for(var i in $scope.clientList) {
                if($scope.selectedClient.id == $scope.clientList[i].id){
                    if($scope.$parent.relationList ) {
                        $scope.clientList[i].relationId =data;
                        $scope.clientList[i].relationName =$scope.selectedClient.relationName;
                        console.log( $scope.clientList[i].tag);
                        $scope.$parent.relationList.push($scope.clientList[i]);
                        $scope.clientList.splice(i,1);
                        //$timeout(function(){
                        //    history.back();
                        //})

                        /*初始化选择的数据*/
                        initInputData();
                        break;
                    }
                    //else{
                    //    $scope.clientList[i].relationId =data;
                    //    $scope.clientList[i].relationName =$scope.selectedClient.relationName;
                    //    $scope.$parent.relationList.push($scope.clientList[i])
                    //    $scope.$parent.relationList = new Array($scope.clientList[i]);
                    //    console.log( $scope.clientList[i].tag);
                    //    $scope.clientList.splice(i,1);
                    //    //$timeout(function(){
                    //    //    history.back();
                    //    //})
                    //    /*初始化选择的数据*/
                    //    initInputData();
                    //    break;
                    //}
                }
            }
        }
    }

    function initInputData(){
        $scope.selectedClient ={id:null,customerName:null,relationName:null};
        $scope.relationName = null;
    }
}])